﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using PromoServer_WPF.Views;
using PromoServer.Library.Entities;
using PromoServer.Library.BLL;

namespace PromoServer_WPF
{
    /// <summary>
    /// Interaction logic for DesignLayoutWindow.xaml
    /// </summary>
    public partial class DesignLayoutWindow : Window
    {
        private PSLayout psLayout;
        private int lastSequence = 0;
        private string currentLayoutType = string.Empty;

        private PromoServer.Library.Entities.Layout currentLayout;
        private bool isNewLayout = true;

        public DesignLayoutWindow(bool isNewLayout)
        {
            InitializeComponent();

            psLayout = new PSLayout();

            if (isNewLayout)
                loadNewLayout();
            else
                loadSavedLayouts();
        }

        private void loadNewLayout()
        {
            // Set new layout flag
            isNewLayout = true;

            // Clear content
            this.mainStackPanel.Children.Clear();

            // Create a new empty layout
            Layout newLayout = new Layout(0, "[Enter Layout Name]", 0);
            currentLayout = newLayout;

            // Bind form to layout
            newLayoutNameGrid.DataContext = newLayout;

            // Set controls
            layoutNameComboBoxLabel.Visibility = Visibility.Hidden;
            nameComboBox.Visibility = Visibility.Hidden;
            newLayoutNameLabel.Visibility = Visibility.Visible;
            nameTextBox.Visibility = Visibility.Visible;
            newLayoutButton.Visibility = Visibility.Hidden;
            cancelButton.Visibility = Visibility.Visible;
        }

        private void loadSavedLayouts()
        {
            // Set new layout flag
            isNewLayout = false;

            // Get layouts
            List<Layout> layouts;
            layouts = psLayout.GetLayouts();

            // Bind form to layout
            CollectionViewSource layoutViewSource = ((CollectionViewSource)(this.FindResource("layoutViewSource")));
            layoutViewSource.Source = layouts;

            layoutNameComboBoxLabel.Visibility = Visibility.Visible;
            nameComboBox.Visibility = Visibility.Visible;
            newLayoutNameLabel.Visibility = Visibility.Hidden;
            nameTextBox.Visibility = Visibility.Hidden;
            newLayoutButton.Visibility = Visibility.Visible;
            cancelButton.Visibility = Visibility.Hidden;
        }

        private void addImageButton_Click(object sender, RoutedEventArgs e)
        {
            LayoutContent emptyLayout = new LayoutContent(lastSequence + 1, 0, true, "image");
            ImageLayoutView layoutView = new ImageLayoutView(emptyLayout, true);

            // Add layout view to main stack panel
            this.mainStackPanel.Children.Add(layoutView);

            // Scroll to new layout
            this.layoutScrollViewer.ScrollToRightEnd();
        }

        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            saveLayout();

            if (isNewLayout)
                loadSavedLayouts();
        }

        private void saveLayout()
        {
            try
            {
                // Save/Update Layout
                int currentLayoutID = 0;
                if (isNewLayout)
                {
                    currentLayoutID = psLayout.SaveLayout(currentLayout);
                    currentLayout.LayoutID = currentLayoutID;
                }
                else
                {
                    currentLayoutID = psLayout.SaveLayout(currentLayout);
                }

                // Save layout contents
                foreach (ImageLayoutView layoutView in mainStackPanel.Children)
                {
                    layoutView.saveLayout(currentLayoutID);
                }
            }
            catch (Exception ex)
            {
                // Display error message to user
                MessageBox.Show(ex.Message);
            }
        }

        private void mainStackPanel_LayoutUpdated(object sender, EventArgs e)
        {
            // MOD : Uncomment below once video panel is functional
            // Set sequence count
            lastSequence = 0;
            for (int i = 0; i < mainStackPanel.Children.Count; i++)
            {
                lastSequence += 1;
            }
        }

        private void addVideoButton_Click(object sender, RoutedEventArgs e)
        {
            VideoLayoutView videoView = new VideoLayoutView();
            this.mainStackPanel.Children.Add(videoView);
            this.layoutScrollViewer.ScrollToRightEnd();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void nameComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // Get/Set current layout
            Layout layout = (Layout)this.nameComboBox.SelectedItem;
            currentLayout = layout;

            // If content exists, load it
            if (currentLayout.Content.Count > 0)
            {
                // Load existing layouts
                this.mainStackPanel.Children.Clear();
                ImageLayoutView imgLayoutView;
                VideoLayoutView vidLayoutView;
                foreach (LayoutContent content in currentLayout.Content)
                {
                    // Add layout view to main stack panel
                    if (content.ContentType.ToLower() == "image")
                    {
                        imgLayoutView = new ImageLayoutView(content, false);
                        this.mainStackPanel.Children.Add(imgLayoutView);
                    }
                    else if (content.ContentType.ToLower() == "video")
                    {
                        vidLayoutView = new VideoLayoutView();
                        this.mainStackPanel.Children.Add(vidLayoutView);
                    }
                }
            }
        }

        private void newLayoutButton_Click(object sender, RoutedEventArgs e)
        {
            loadNewLayout();
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            loadSavedLayouts();
        }
    }
}
